/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package roomarrange;

import com.sun.data.provider.impl.CachedRowSetDataProvider;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.Button;
import com.sun.webui.jsf.component.Checkbox;
import com.sun.webui.jsf.component.DropDown;
import com.sun.webui.jsf.component.Listbox;
import com.sun.webui.jsf.component.TextArea;
import com.sun.webui.jsf.component.TextField;
import com.sun.webui.jsf.model.list.ListItem;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.convert.IntegerConverter;
import javax.sql.DataSource;
import javax.sql.rowset.CachedRowSet;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version main2.java
 * @version Created on 2009-9-21, 22:36:23
 * @author ben
 */
public class main2 extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
        t_roomsetDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.t_roomsetRowSet}"));
        t_grpsetDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.t_grpsetRowSet1}"));
        v_usrDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.v_usrRowSet}"));
        v_usrListADataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.v_usrListA}"));
        v_attendeeDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.v_attendeeRowSet}"));
    }
    private CachedRowSetDataProvider t_roomsetDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getT_roomsetDataProvider() {
        return t_roomsetDataProvider;
    }

    public void setT_roomsetDataProvider(CachedRowSetDataProvider crsdp) {
        this.t_roomsetDataProvider = crsdp;
    }
    private IntegerConverter dropDown1Converter = new IntegerConverter();

    public IntegerConverter getDropDown1Converter() {
        return dropDown1Converter;
    }

    public void setDropDown1Converter(IntegerConverter ic) {
        this.dropDown1Converter = ic;
    }
    private DropDown dropDown1 = new DropDown();

    public DropDown getDropDown1() {
        return dropDown1;
    }

    public void setDropDown1(DropDown dd) {
        this.dropDown1 = dd;
    }
    private CachedRowSetDataProvider t_grpsetDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getT_grpsetDataProvider() {
        return t_grpsetDataProvider;
    }

    public void setT_grpsetDataProvider(CachedRowSetDataProvider crsdp) {
        this.t_grpsetDataProvider = crsdp;
    }
    private IntegerConverter dropDown2Converter = new IntegerConverter();

    public IntegerConverter getDropDown2Converter() {
        return dropDown2Converter;
    }

    public void setDropDown2Converter(IntegerConverter ic) {
        this.dropDown2Converter = ic;
    }
    private CachedRowSetDataProvider v_usrDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getV_usrDataProvider() {
        return v_usrDataProvider;
    }

    public void setV_usrDataProvider(CachedRowSetDataProvider crsdp) {
        this.v_usrDataProvider = crsdp;
    }
    private IntegerConverter dropDown3Converter = new IntegerConverter();

    public IntegerConverter getDropDown3Converter() {
        return dropDown3Converter;
    }

    public void setDropDown3Converter(IntegerConverter ic) {
        this.dropDown3Converter = ic;
    }
    private CachedRowSetDataProvider v_usrListADataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getV_usrListADataProvider() {
        return v_usrListADataProvider;
    }

    public void setV_usrListADataProvider(CachedRowSetDataProvider crsdp) {
        this.v_usrListADataProvider = crsdp;
    }
    private CachedRowSetDataProvider v_attendeeDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getV_attendeeDataProvider() {
        return v_attendeeDataProvider;
    }

    public void setV_attendeeDataProvider(CachedRowSetDataProvider crsdp) {
        this.v_attendeeDataProvider = crsdp;
    }
    private IntegerConverter listbox1Converter = new IntegerConverter();

    public IntegerConverter getListbox1Converter() {
        return listbox1Converter;
    }

    public void setListbox1Converter(IntegerConverter ic) {
        this.listbox1Converter = ic;
    }
    private DropDown dropDown2 = new DropDown();

    public DropDown getDropDown2() {
        return dropDown2;
    }

    public void setDropDown2(DropDown dd) {
        this.dropDown2 = dd;
    }
    private DropDown dropDown3 = new DropDown();

    public DropDown getDropDown3() {
        return dropDown3;
    }

    public void setDropDown3(DropDown dd) {
        this.dropDown3 = dd;
    }
    private TextField textField3 = new TextField();

    public TextField getTextField3() {
        return textField3;
    }

    public void setTextField3(TextField tf) {
        this.textField3 = tf;
    }
    private TextArea textArea1 = new TextArea();

    public TextArea getTextArea1() {
        return textArea1;
    }

    public void setTextArea1(TextArea ta) {
        this.textArea1 = ta;
    }
    private TextField textField4 = new TextField();

    public TextField getTextField4() {
        return textField4;
    }

    public void setTextField4(TextField tf) {
        this.textField4 = tf;
    }
    private Listbox listbox1 = new Listbox();

    public Listbox getListbox1() {
        return listbox1;
    }

    public void setListbox1(Listbox l) {
        this.listbox1 = l;
    }
    private Button button2 = new Button();

    public Button getButton2() {
        return button2;
    }

    public void setButton2(Button b) {
        this.button2 = b;
    }
    private Checkbox checkbox1 = new Checkbox();

    public Checkbox getCheckbox1() {
        return checkbox1;
    }

    public void setCheckbox1(Checkbox c) {
        this.checkbox1 = c;
    }

    // </editor-fold>
    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public main2() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("main2 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here

    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
        int li_host;
        int li_init;
        li_host = this.getSessionBean1().getIMtHost();
        li_init = this.getSessionBean1().getIMtInitdept();
        
        this.getDropDown2().setSelected(li_init);
        this.getDropDown3().setSelected(li_host);
        this.getDropDown1().setSelected(this.getSessionBean1().getIroomid());
        Object mtId = 0;
        mtId = this.getSessionBean1().getSUUID();
        try {
            CachedRowSet crs = this.getSessionBean1().getV_attendeeRowSet();
            crs.setObject(1, mtId);
            crs.execute();
        } catch (SQLException ex) {
            Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
        t_roomsetDataProvider.close();
        t_grpsetDataProvider.close();
        v_usrDataProvider.close();
        v_usrListADataProvider.close();
        v_attendeeDataProvider.close();
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }


    public String button1_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        int li_host;
        int li_init;
        li_host = (Integer) this.getDropDown3().getSelected();
        li_init = (Integer) this.getDropDown2().getSelected();
        this.getSessionBean1().setIMtHost(li_host);
        this.getSessionBean1().setIMtInitdept(li_init);
        return "toChoice";
    }

    public String button2_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        //提交并确认
        int li_host;
        int li_init;
        int li_bj = 0;
        int li_status = 0;
        int li_roomid;
        int li_roomsort;
        int li_usrid=0;
        int li_confirmbygrpid=0;
        int li_video=0;
        String sName;
        String sAbstract;
        String sNotice;
        String sTip = null;
        String sDate = null;
        String eDate = null;
        String sRoomName = null;
        String sRef=null;
        li_host = (Integer) this.getDropDown3().getSelected();
        li_init = (Integer) this.getDropDown2().getSelected();
        sName = this.getTextField3().getText().toString();
        sAbstract = this.getTextArea1().getText().toString();
        sNotice = this.getTextField4().getText().toString();
        sDate = this.getSessionBean1().getSSdate();
        eDate = this.getSessionBean1().getSEdate();
        li_usrid = this.getSessionBean1().getIusrid();
        sRef = this.getSessionBean1().getSRef();
        if (sName == null) {
            info("会议名称不能为空，请填写完整！");
            return null;
        }
        if (sAbstract == null) {
            info("会议摘要不能为空，请填写完整！");
            return null;
        }
        if (sNotice == null) {
            info("会议通知不能为空，请填写完整！");
            return null;
        }
        if((Boolean)this.getCheckbox1().getSelectedValue() == true){
            li_video=1;
        }
        Iterator it = this.getListbox1().getListItems();
        ListItem ls_tmp;
        while (it.hasNext()) {
            int li_tmp;

            ls_tmp = (ListItem) it.next();
            li_tmp = Integer.parseInt(ls_tmp.getValue());
            if (li_tmp == li_host) {
                li_bj = 1;
                break;
            }
        }
        if (li_bj == 0) {
            info("会议主持人未被选择进参加会议人员，请选择完整！");
            return null;
        }
        li_roomid = this.getSessionBean1().getIroomid();
        //更新
        String sMeetingid = this.getSessionBean1().getSUUID();
        //生成临时数据
        ApplicationContext ctx = new ClassPathXmlApplicationContext("SpringConfig.xml");
        DataSource ds = (DataSource) ctx.getBean("dataSourceDBDirect");
        //DataSource ds = (DataSource) ctx.getBean("dataSourcePool");
        DataJdbcDao taskDao = new DataJdbcDao();
        taskDao.setDataSource(ds);
        //取会议室类别
        li_roomsort = taskDao.getRoomSort(li_roomid);
        sRoomName = taskDao.getRoomName(li_roomid);
        
        switch (li_roomsort) {
            case 1:
                //办公室审批
                li_status = 2;
                sTip = "会议已成功申请，正等待办公室审批！";
                li_confirmbygrpid=19;
                break;
            case 2:
                //先到先得
                li_status = 5;
                sTip = "会议已成功申请，请准时参加！";
                break;
            case 3:
                //技术科审批
                li_status = 1;
                sTip = "会议已成功申请，正等待技术科审批！";
                li_confirmbygrpid=13;
                break;
        }
        //更新会议数据
        taskDao.updateMeeting(sMeetingid, li_host, li_init, sName, sAbstract, sNotice, li_status,li_usrid,li_roomsort,li_confirmbygrpid);
        if (li_video==1){
            taskDao.updateMeeting(sMeetingid, li_video);
        }
        //向qq发信息,如果是直接确认的申请才发消息
        if (sRef.equalsIgnoreCase("main")){
            //只有从mian页过来的请求才是新增加的，其他的是修改的请求
            //插入的时候发消息，要将bj设置为1
            //删除的时候发消息，要将bj设置为－1
            int ibj=1;
            meetings mt = new meetings();
            String sapp = this.getSessionBean1().getSApp();
            mt = taskDao.getMeeting(sMeetingid,sapp);
            setupParam stp = new setupParam();
            stp = taskDao.getParam();
            taskDao.sendRTX(mt,ibj,stp);
            //给开门人发消息
            if(li_roomsort==2&&li_status==5){
                ibj=5;
                taskDao.sendRTX(mt,ibj,stp);
            }
        }
        //显示提示信息
        this.getSessionBean1().setSTip(sTip);
        //cls
        this.getSessionBean1().setIMtHost(0);
        this.getSessionBean1().setIMtInitdept(0);
        this.getSessionBean1().setIroomid(0);
        this.getSessionBean1().setSEdate("");
        this.getSessionBean1().setSSdate("");
        this.getSessionBean1().setSUUID("");
        this.getSessionBean1().setSMtAbstract("");
        this.getSessionBean1().setSMtName("");
        this.getSessionBean1().setSMtNotice("");
        this.getSessionBean1().setSRefMtId("");

        return "toSucc";
    }
}

